#########################################################################
#									#
#									#
#  Makefile for the Intel Galileo version of the Xinu operating system	#
#									#
#  includes: .deps and .defs						#
#									#
#									#
#########################################################################

COMPILER_ROOT	=	/usr/bin/
PNAME		=	QEMU
PLAT		=	Platform_$(PNAME)
TOPDIR		=	..

CC		=	${COMPILER_ROOT}gcc
LD		=	${COMPILER_ROOT}ld
OBJCOPY		=	${COMPILER_ROOT}objcopy
XINU		=	$(TOPDIR)/compile/xinu.elf
BUILDMAKE	=	$(TOPDIR)/compile/bin/build-make

MAKEDEP		=	$(CC) -M -MG

DEPSFILE	=	.deps
DEFSFILE	=	.defs
VERSIONFILE	=	version
LDSCRIPT	=	ld.script
MKVERS		=	$(TOPDIR)/compile/bin/mkvers

REBUILDFLAGS	=	-s $(TOPDIR)/system  debug.c		\
			-s $(TOPDIR)/lib			\
			-s $(TOPDIR)/device/tty			\
			-s $(TOPDIR)/device/nam			\
			-s $(TOPDIR)/device/ram			\
			-s $(TOPDIR)/device/lfs			\
			-s $(TOPDIR)/device/kbd			\
			-s $(TOPDIR)/device/vga			\
			-s $(TOPDIR)/shell  

INCLUDE		=	-I$(TOPDIR)/include
DEFS		= 	-DBSDURG -DVERSION=\""`cat $(VERSIONFILE)`"\"

# Compiler flags
CFLAGS  = -march=i586 -m32 -g3 -fno-builtin -fno-stack-protector -nostdlib -c -Wall -O0 ${DEFS} ${INCLUDE}
# CFLAGS  = -march=i586 -m32 -fno-builtin -fno-stack-protector -nostdlib -c -Wall -O0 ${DEFS} ${INCLUDE}
SFLAGS  = ${INCLUDE}

# Loader flags
LDFLAGS = -dn -m elf_i386 -Map xinu.map -T $(LDSCRIPT)

all:	$(XINU)

#--------------------------------------------------------------------------------
# Handle generation of a new version string when initialize is recompiled
#--------------------------------------------------------------------------------

newversion:
	@echo creating new version
	@$(MKVERS) $(PNAME) > $(VERSIONFILE)

#--------------------------------------------------------------------------------
# Include generic make targets and rules from the file generated by build-make
#--------------------------------------------------------------------------------
-include $(DEFSFILE)

#--------------------------------------------------------------------------------
# Add files that require special rules and place start.o at the front
#--------------------------------------------------------------------------------
LD_LIST = binaries/start.o $(filter-out binaries/start.o,$(OBJ_FILES))

#------------------------------------------------------------
# Rules for files that need special handling
#------------------------------------------------------------

# Define variables for the Configuration file and generated files

CONFFILE	= $(TOPDIR)/config/Configuration
CONFH		= $(TOPDIR)/include/conf.h
CONFC		= $(TOPDIR)/system/conf.c

# Define variables for the config program and its sources

CONFPGM		= $(TOPDIR)/config/config
CONFL		= $(TOPDIR)/config/config.l
CONFY		= $(TOPDIR)/config/config.y

# Set up the required build directory structure

BLDDIRS	= binaries
export

#--------------------------------------------------------------------------------
# Specific additional rules and exceptions
#--------------------------------------------------------------------------------

$(XINU):	Makefile rebuild $(BLDDIRS) $(DEFSFILE) $(DEPSFILE) $(CONFH) $(CONFC) $(LD_LIST)
	@echo;echo 'Loading object files to produce GRUB bootable xinu'
	@$(LD) $(LDFLAGS) $(LD_LIST) -o $(XINU)
	@echo

$(BLDDIRS): 
	@mkdir -p $(BLDDIRS)

objects: $(LD_LIST)

$(CONFH): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFH)
	@make configure

$(CONFC): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFC)
	@make configure

$(CONFPGM): $(CONFL) $(CONFY)
	@echo making the config program
	@make -C $(TOPDIR)/config clean all install

configure:
	@echo forcing a rebuild of conf.h and conf.c
	@make newversion
	@(cd $(TOPDIR)/config; make install)

clean:
	@echo removing .o files
	@rm -f ${LD_LIST}
	@echo   removing configuration files ...
	@rm -f $(CONFH) $(CONFC)
	@(cd $(TOPDIR)/config; make clean)
	@echo removing xinu ...
	@rm -f $(XINU)

#--------------------------------------------------------------------------------
# Locations of source directories and exceptions (.c and .[sS] files to exclude)
#--------------------------------------------------------------------------------
	
$(DEFSFILE):
	@rm -f $(DEFSFILE)
	@echo "" > $(DEFSFILE)
	@make rebuild

$(DEPSFILE):
	@rm -f $(DEPSFILE)
	@echo "" > $(DEPSFILE)
	@make depend

rebuild: $(CONFC)
	@echo Rebuilding the $(DEFSFILE) file
	@$(BUILDMAKE) $(REBUILDFLAGS) > $(DEFSFILE)

defclean:
	rm -f $(DEFSFILE)
	echo "" > $(DEFSFILE)

depend: $(DEFSFILE)
	@echo;echo Making all dependencies in $(DEPSFILE)
	@$(MAKEDEP) ${INCLUDE} ${SRC_FULL} > $(DEPSFILE)
	@echo;echo Finished making dependencies

depclean:
	rm -f $(DEPSFILE)
	echo "" > $(DEPSFILE)

-include $(DEPSFILE)
